/*
 * Created with IDEA.
 * Description: A fully open-sourced high performance unibody close-loop driver for
 * Step-Motor, which was used in my DummyRobot project:
 * https://github.com/peng-zhihui/Dummy-Robot
 * Based on XDrive project: https://github.com/unlir/XDrive
 * @Author: Pengzhihui
 * @Date: ${DATE} ${TIME}
 *
 */

//
// Created by deng on 2022/12/25.
//

#ifndef MOTOR_S_COMMON_INC_H
#define MOTOR_S_COMMON_INC_H
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------- C Scope ---------------------------*/
#include <stdbool.h>
#include "stdint-gcc.h"
#include "main.h"

#define SOFT_VER_A 1
#define SOFT_VER_B 1

//#define HARD_VER_INIT IMU6
#define HARD_VER_INIT IRC
//#define HARD_VER_INIT DUMMY
//#define HARD_VER_INIT AR3
//#define HARD_VER_INIT SITO
//#define HARD_VER_INIT BATTLE_CHARGE_THERE_AXIS
//#define HARD_VER_INIT MR6
//#define HARD_VER_INIT TRANSMIT_PLANE_THERE_AXIS

#if defined(Driver_IMU6) || defined(Driver_IRC)
typedef struct{
    bool Find_Home;
    uint8_t Elector1;
    uint8_t Elector2;
    uint8_t Color_Flag;
    uint8_t Start_Led;
}IMU6_SIGNAL;
extern IMU6_SIGNAL imu6_data;
#endif

#define CAN_NODE_ID 5

#define HARD_VER_SERIES_NUM 0
#define HARD_VER_ROBOT_NUM 3

#define FIND_HOME_DRR_PERMIT 100

void Main();
void OnUartCmd(uint8_t* _data, uint16_t _len);
void OnCanCmd(uint8_t _id,uint8_t _cmd, uint8_t* _data, uint32_t _len);

void AplyHomeWithPhysicLimit();
bool IsCalibratorKeyOn();
bool IsCalibratorOtherKeyOn();
extern bool IsSynMotor(uint8_t id);

enum NEED_FIND_HOME_FLAG{
    FIND_HOME_WITHOUT_APPLY = 255,
    IS_NOT_NEED = 0,
    FIND_HOME_WITH_IMU,
    FIND_HOME_WITH_PHYSICAL_LIMIT,
    FIND_HOME_WITH_PHYSICAL_KEY,
};

enum S_UP_OR_DOWN{
    S_UP,
    S_CONSTANT_VELOCITY,
    S_DOWN,
};

typedef enum HARD_VER{
    HARD_VER_ERR = 0,
    DUMMY,
    AR3,
    SITO,
    BATTLE_CHARGE_THERE_AXIS,
    MR6,
    TRANSMIT_PLANE_THERE_AXIS,
    HARD_VER_SUM_IN_THIS_SOFT_VER,
    IMU6,
    IRC,
}Enum_16bit;

extern enum NEED_FIND_HOME_FLAG NeedFindHomeFalg,LastNeedFindHomeFlag;

extern bool FindedHomeFlag,FindHomeFlagUpdataFlag,FindHomeNeedReverseFlag,UpdatedToFlashFlag;
extern uint32_t FindHomeRealPos;
extern uint16_t FindHomeNeedBackMs;

extern int32_t AccFIdx;
extern uint16_t Stimes;
extern enum S_UP_OR_DOWN SUpOrDown;
extern int32_t trackVelocityAdjustVal;

extern uint32_t TofDis;

#ifdef __cplusplus
}
/*---------------------------- C++ Scope ---------------------------*/
#include <cstdio>
#include "motor.h"
#include "mt6816_stm32.h"
#include "tb67h450_stm32.h"
#include "encoder_calibrator_stm32.h"
#include "button_stm32.h"
#include "led_stm32.h"

#endif
#endif //MOTOR_S_COMMON_INC_H
